package cn.skcks.controller;

import cn.skcks.pojo.User;
import cn.skcks.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Controller
// RestController 标注的类下只会以字符串形式返回数据, 不会经过视图解析器
@RestController
@SuppressWarnings(value = {"all"})
public class UserController {

	// 此注解不会被视图解析器解析, 直接返回一个字符串
	@ResponseBody
	@RequestMapping(value = "/json1", produces = "application/json;charset=utf-8")
	public String json1() throws JsonProcessingException {

		User user = new User("时空", 10000);

		ObjectMapper objectMapper = new ObjectMapper();

		return objectMapper.writeValueAsString(user);
	}

	@RequestMapping(value = "/json2")
	public String json2() throws JsonProcessingException {

		User user1 = new User("时空", 10000);
		User user2 = new User("时间", 10001);
		User user3 = new User("时光", 10002);

		List<User> userList = new ArrayList<User>();
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		ObjectMapper objectMapper = new ObjectMapper();

		return objectMapper.writeValueAsString(userList);
	}

	@RequestMapping(value = "/json3")
	public String json3() throws JsonProcessingException {
		Date date = new Date();

		ObjectMapper objectMapper = new ObjectMapper();

		// ObjectMapper 解析 Date 对象后的默认格式为 TimeStamp 时间戳
		return objectMapper.writeValueAsString(date);
	}

	@RequestMapping(value = "/json4")
	public String json4() throws JsonProcessingException {
		Date date = new Date();

		ObjectMapper objectMapper = new ObjectMapper();

		// 自定义日期格式
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String dateTime = simpleDateFormat.format(date);

		return objectMapper.writeValueAsString(dateTime);
	}

	@RequestMapping(value = "/json5")
	public String json5() throws JsonProcessingException {
		Date date = new Date();

		// 通过 objectMapper 转换时间格式
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);

		// 自定义日期格式
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		objectMapper.setDateFormat(simpleDateFormat);

		return objectMapper.writeValueAsString(date);
	}

	@RequestMapping(value = "/json6")
	public String json6() {
		Date date = new Date();
//		return JsonUtils.getJson(date);
		return JsonUtils.getJson(date,"yyyy年MM月dd日 HH:mm:ss");
	}

	@RequestMapping("/fJson")
	public String fJson(){
		User user1 = new User("时空", 10000);
		User user2 = new User("时间", 10001);
		User user3 = new User("时光", 10002);

		List<User> userList = new ArrayList<User>();
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		return JSON.toJSONString(userList);
	}
}